package cn.mzcode.xyzpfuwuduan.controller;



import cn.mzcode.xyzpfuwuduan.common.Const;
import cn.mzcode.xyzpfuwuduan.common.ResponseCode;
import cn.mzcode.xyzpfuwuduan.common.ServerResponse;
import cn.mzcode.xyzpfuwuduan.pojo.Student;
import cn.mzcode.xyzpfuwuduan.service.IStudentService;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/student/")
public class StudentController {

    @Autowired
    private IStudentService iStudentService;


    /**
     * 用户登录
     * @param username
     * @param password
     * @param id
     */
    @RequestMapping(value = "login.do",method =RequestMethod.POST)
    @ResponseBody
    public ServerResponse<Student> login(String username, String password, Integer id){
        return iStudentService.login(username,password,id);
    }

    /**
     * 退出登录
     * @param session
     * @return
     */
    @RequestMapping(value = "logout.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> logout(HttpSession session){
        session.removeAttribute(Const.CURRENT_USER);
        return ServerResponse.createBySuccess();
    }

    /**
     * 注册
     * @param student
     * @return
     */

    @RequestMapping(value ="register.do",method = RequestMethod.POST)
    @ResponseBody
    public  ServerResponse<String> register(Student student){
        return iStudentService.register(student);
    }


    /**
     * 校验
     * @param str
     * @param type
     * @return
     */
    @RequestMapping(value = "check_Vlaid.do",method=RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> checkVlaid(String str,String type){
        return iStudentService.checkVlaid(str,type);
    }

//    /**
//     * 获取登录用户信息
//     * @param session
//     * @return
//     */
//    @RequestMapping(value = "get_Student_Info.do",method = RequestMethod.POST)
//    @ResponseBody
//    public ServerResponse<Student> getUserInfo(HttpSession session){
//        Student student=(Student)session.getAttribute(Const.CURRENT_USER);
//        if (student!=null){
//            return ServerResponse.createBySuccess(student);
//        }
//        return ServerResponse.createByErrorMessage("用户未登录，无法获得当前信息");
//    }


//    /**
//     * 忘记密码下的重置密码
//     * @param username
//     * @param passwordNew
//     * @param phone
//     * @return
//     */
//    @RequestMapping(value = "forget_reset_password.do",method=RequestMethod.POST)
//    @ResponseBody
//    public ServerResponse<String> forgetResetPassword(String username,String passwordNew,String phone){
//        return iStudentService.forgetResetPassword(username,passwordNew,phone);
//    }

    /**
     * 登录状态下重置密码
     * @param studentId
     * @param passwordOld
     * @param passwordNew
     * @return
     */
    @RequestMapping(value = "reset_password.do",method=RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> resetPassword(Integer studentId, String passwordOld, String passwordNew){
        return iStudentService.resetPassword(passwordOld,passwordNew,studentId);
    }

    /**
     * 更新用户信息
     * @param studentId
     * @return
     */
    @RequestMapping(value = "update_information.do",method=RequestMethod.POST)
    @ResponseBody
    public ServerResponse<Student> updateInformation(Integer studentId){
//        Student currentuser= (Student) session.getAttribute(Const.CURRENT_USER);
//       if(currentuser==null){
//           return ServerResponse.createByErrorMessage("用户未登录");
//       }
//        student.setId(currentuser.getId());
//        student.setUsername(currentuser.getUsername());
//        ServerResponse<Student> response=iStudentService.updateInformation(studnetId);
//        if(response.isSuccess()){
//            session.setAttribute(Const.CURRENT_USER,response.getData());
//        }
//        return response;
        return iStudentService.updateInformation(studentId);
    }

    /**
     * 获取个人详细信息
     * @param id
     * @return
     */
    @RequestMapping(value = "get_information.do",method=RequestMethod.POST)
    @ResponseBody
    public ServerResponse<Student> getInformation(Integer id){

        return iStudentService.getInformation(id);
    }



}
